package com.ruoyi.office.asset.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.office.asset.domain.AssetStatus;
import com.ruoyi.office.asset.mapper.AssetStatusMapper;
import com.ruoyi.office.asset.request.AssetStatusQueryDto;
import com.ruoyi.office.asset.service.IAssetStatusService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 资产状态服务实现类
 *
 * @author tqfeiyang
 * @date 2024-12-11
 */
@Service
@RequiredArgsConstructor
public class AssetStatusServiceImpl implements IAssetStatusService
{
    private final AssetStatusMapper assetStatusMapper;

    /**
     * 查询资产状态列表
     *
     * @param criteria 查询条件
     * @return 资产状态列表
     */
    @Override
    public List<AssetStatus> selectAssetStatusList(AssetStatusQueryDto criteria)
    {
        return assetStatusMapper.selectAssetStatusList(criteria);
    }

    /**
     * 查询资产状态
     *
     * @param id 资产状态主键
     * @return 资产状态
     */
    @Override
    public AssetStatus selectAssetStatusById(Integer id)
    {
        return assetStatusMapper.selectAssetStatusById(id);
    }

    /**
     * 新增资产状态
     *
     * @param assetStatus 资产状态
     * @return 影响的行数
     */
    @Override
    @Transactional
    public int insertAssetStatus(AssetStatus assetStatus)
    {
        if (StringUtils.isNotEmpty(assetStatus.getTitle()))
        {
            if (assetStatusMapper.checkIfTitleExists(assetStatus.getTitle(), null))
            {
                throw new BusinessException("名称重复");
            }
        }
        assetStatus.setCreateBy(SecurityUtils.getUsername());
        assetStatus.setCreateAt(LocalDateTime.now());
        assetStatus.setUpdateBy(SecurityUtils.getUsername());
        assetStatus.setUpdateAt(LocalDateTime.now());
        return assetStatusMapper.insertAssetStatus(assetStatus);
    }

    /**
     * 修改资产状态
     *
     * @param assetStatus 资产状态
     * @return 影响的行数
     */
    @Override
    @Transactional
    public int updateAssetStatus(AssetStatus assetStatus)
    {
        if (StringUtils.isNotEmpty(assetStatus.getTitle()))
        {
            if (assetStatusMapper.checkIfTitleExists(assetStatus.getTitle(), assetStatus.getId()))
            {
                throw new BusinessException("名称重复");
            }
        }
        assetStatus.setUpdateBy(SecurityUtils.getUsername());
        assetStatus.setUpdateAt(LocalDateTime.now());
        return assetStatusMapper.updateAssetStatus(assetStatus);
    }

    /**
     * 删除资产状态信息
     *
     * @param id 资产状态主键
     * @return 影响的行数
     */
    @Override
    @Transactional
    public int deleteAssetStatusById(Integer id)
    {
        return assetStatusMapper.deleteAssetStatusById(id);
    }

    /**
     * 批量删除资产状态
     *
     * @param ids 需要删除的资产状态主键
     * @return 影响的行数
     */
    @Override
    public int deleteAssetStatusByIds(Integer[] ids)
    {
        return assetStatusMapper.deleteAssetStatusByIds(ids);
    }
}
